<?php
namespace jy_saleapp;
defined('BY_JYA') or exit('error');
/*
*/
class member extends \plugin{
	public function upfeelog($member=array(),$appid=0){
		//$basic=$this->plugin_setting('basic');
		if(!$member['fromuser']){
			return false;
		}
		$this->yaolog($appid,$member['fromuser']);
		$app=pdo_get('jy_saleapp_list',array('id'=>$appid,'uniacid'=>$_SESSION['uniacid']));
		$app['saleset']=json_decode($app['saleset'],true);
		foreach($app['saleset'] as $k=>$yv){
				if($yv['fee']>0 && $yv['type']){
					if($yv['yao_jitype']==1){
						$isgetfee=$this->pdo_count('feelog',array('mid'=>$member['fromuser'],'newmid'=>$_SESSION['uid'],'appid'=>$appid,'uniacid'=>$_SESSION['uniacid']));
						if($isgetfee){
							continue;
						}
					}else{
						$isgetfee=$this->pdo_count('feelog',array('salekey'=>$k,'mid'=>$member['fromuser'],'newmid'=>$_SESSION['uid'],'appid'=>$appid,'uniacid'=>$_SESSION['uniacid']));
						if($isgetfee){
							continue;
						}
						$num=$this->pdo_count('yaolog',array('fromuser'=>$member['fromuser'],'appid'=>$appid,'uniacid'=>$_SESSION['uniacid']));
						if(!$yv['num'] || $yv['num']>$num){
							continue;
						}
					}
					$insert=array(
						'mid'=>$member['fromuser'],
						'newmid'=>$_SESSION['uid'],
						'uniacid'=>$_SESSION['uniacid'],
						'appid'=>$appid,
						'salekey'=>$k,
						'fee'=>$yv['fee'],
						'type'=>$yv['type'],
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_saleapp_feelog',$insert);
					model('core/member')->member_credit($member['fromuser'],'credit'.$yv['type'],$yv['fee'],true,'jy_saleapp','邀请注册APP奖励');
				}
		}
		return true;
	}
	public function uplog($appid=0,$fromuser=0){
		$log=pdo_get('jy_saleapp_log',array('appid'=>$appid,'mid'=>$_SESSION['uid'],'fromuser'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		if($log){
			return false;
		}
		$insert=array(
			'mid'=>$_SESSION['uid'],
			'uniacid'=>$_SESSION['uniacid'],
			'fromuser'=>$fromuser,
			'appid'=>$appid,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_saleapp_log',$insert);
		return true;
	}
	public function yaolog($appid=0,$fromuser=0){
		$log=pdo_get('jy_saleapp_yaolog',array('appid'=>$appid,'mid'=>$_SESSION['uid'],'fromuser'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		if($log){
			return false;
		}
		$insert=array(
			'mid'=>$_SESSION['uid'],
			'uniacid'=>$_SESSION['uniacid'],
			'fromuser'=>$fromuser,
			'appid'=>$appid,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_saleapp_yaolog',$insert);
		return true;
	}
}